package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare;

import android.util.Log;

import com.coffeeloversupportlibrary.coffeelover.ApiService;
import com.coffeeloversupportlibrary.coffeelover.callback.ModelCallBack;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.DailyContent;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.DailyPicture;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.OriginDailyContent;
import com.coffeeloversupportlibrary.core.base.BaseModel;
import com.coffeeloversupportlibrary.core.data.IDataRepository;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author puyv
 * @createTime 2021/11/14 0:13
 * @projectName CoffeeLoverSupportLibrary
 * @className DailyShareModel.java
 * @description TODO
 */
public class DailyShareModel extends BaseModel {

    public DailyShareModel(IDataRepository dataRepository) {
        super(dataRepository);
    }

    public void getShareList(ModelCallBack<List<DailyContent>,String> callback){
            getRetrofitService(ApiService.class)
                    .requestShareList()
                    .enqueue(new Callback<OriginDailyContent>() {
                        // TODO: 2021/11/23 双重循环 解决分享内容的图片数组赋值
                        @Override
                        public void onResponse(Call<OriginDailyContent> call, Response<OriginDailyContent> response) {
                              if(response.body().getSuccess()){
                                  List<DailyContent> shares=new ArrayList<>();
                                  for(OriginDailyContent.DataDTO.ListDTO listDTO:response.body().getShareList()){

                                      DailyContent dailyContent =new DailyContent(listDTO.getId(),listDTO.getUserId(), listDTO.getUserName(),
                                              listDTO.getShareContent(),listDTO.getCreateTime(), listDTO.getCount(), listDTO.getUserImg());
                                      // TODO: 2021/11/30  排查问题   username create time 字段空
                                      // 结合上传考虑
                                      List<DailyPicture> picList=new ArrayList<>();

                                      for(OriginDailyContent.DataDTO.ListDTO.PicListDTO pic:listDTO.getPicList()){
                                          DailyPicture dailyPicture =new DailyPicture(pic.getId(), pic.getShareId(), pic.getShareImg(),pic.getImgOrder());
                                          picList.add(dailyPicture);
                                      }

                                      dailyContent.setPictures(picList);
                                      shares.add(dailyContent);
                                  }

                                  callback.onSuccess(shares);

                              }else{
                                  callback.Failure(response.body().getMessage());
                              }
                        }

                        @Override
                        public void onFailure(Call<OriginDailyContent> call, Throwable t) {
                            callback.Failure(t.getMessage());
                        }
                    });
    }



}
